package com.easywan.web.controller.gtd;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easywan.core.BaseController;
import com.easywan.pojo.IResult;
import com.easywan.pojo.IResultSupport;
import com.easywan.pojo.PageRow;
import com.easywan.service.IGtdService;
import com.easywan.service.IToolsNameService;
import com.easywan.vo.Gtd;

@Controller
@RequestMapping("users")
public class GTDController extends BaseController{
	Logger logger = Logger.getLogger(GTDController.class);
	@Autowired
	IGtdService gtdService;
	@Autowired
	IToolsNameService toolsNameService;
	
	/**
	 * @time 2017年8月20日19:59:43
	 * 添加用戶在未登錄 與未添加該工具的時候
	 * @param request
	 * @return
	 */
	@RequestMapping("work/gtd")
	public String index(HttpServletRequest request){
		if(null == getUserInfo(request,"")){
			return "redirect:/introduce/gtd.html";
		}
		try{
			if(null == toolsNameService.getRToolsName(getUsersId(request),1)){
				return "redirect:/introduce/gtd.html";
			}
		}catch (Exception e){
			return "redirect:/introduce/gtd.html";
		}
		return "users/work/gtd/gtd.jsp";
	}
	@ResponseBody
	@RequestMapping("gtd")
	public String gtd(HttpServletRequest request,PageRow pageRow){
		IResult result = null;
		try{
			result = gtdService.getUsersGtd(getUsersId(request),pageRow);
		}catch(Exception e){
			logger.error(e);
			result = IResultSupport.buildResult(IResult.EXCEPTION_CODE, "异常！");
		}
		return toJSON(result);
	}
	@ResponseBody
	@RequestMapping("gtdDoing")
	public String gtdDoing(HttpServletRequest request){
		IResult result = null;
		try{
			String doing = getString(request,"doing");
			result = gtdService.getUsersDoingGtd(getUsersId(request),doing);
		}catch(Exception e){
			result = IResultSupport.buildResult(IResult.EXCEPTION_CODE, "异常！");
		}
		return toJSON(result);
	}
	@ResponseBody
	@RequestMapping("gtdDone")
	public String gtdDone(HttpServletRequest request){
		IResult result = null;
		try{
			String done = getString(request,"done");
			result = gtdService.getUsersDoneGtd(getUsersId(request),done);
		}catch(Exception e){
			result = IResultSupport.buildResult(IResult.EXCEPTION_CODE, "异常！");
		}
		return toJSON(result);
	}
	
	@ResponseBody
	@RequestMapping("gtdGone")
	public String gtdGone(HttpServletRequest request){
		IResult result = null;
		try{
			String gone = getString(request,"gone");
			result = gtdService.getUsersGoneGtd(getUsersId(request),gone);
		}catch(Exception e){
			result = IResultSupport.buildResult(IResult.EXCEPTION_CODE, "异常！");
		}
		return toJSON(result);
	}
	
	@ResponseBody
	@RequestMapping("gtdRabit")
	public String gtdRabit(HttpServletRequest request){
		IResult result = null;
		try{
			String status = getString(request,"status");
			result = gtdService.getUsersRabitGtd(getUsersId(request),status);
		}catch(Exception e){
			result = IResultSupport.buildResult(IResult.EXCEPTION_CODE, "异常！");
		}
		return toJSON(result);
	}
	
	/**
	 * gtd添加页面
	 * @url url http://tool.easywan.info/users/gtdIdeaAdd.html
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("gtdIdeaAdd")
	public String gtdIdeaAdd(HttpServletRequest request){
		return toPath("users/work/gtd/idea/add.jsp");
	}
	@ResponseBody
	@RequestMapping("gtd/addGtd")
	public String addGtd(HttpServletRequest request){
		IResult rst = null;
		try{
		String name = getString(request,"name");
		String content = getString(request,"content");
		String startDate = getString(request,"startDate");
		String endDate = getString(request,"endDate");
		paramsNotNull(name);
		Integer uid = getUsersId(request);
		rst = gtdService.addGtd(name,content,uid,startDate,endDate);
		}catch(Exception e){
			logger.error(e.getMessage());
			rst = IResultSupport.buildResult(IResult.FAILT_CODE, e.getMessage());
		}
		return toJSON(rst);
	}
	/**
	 * 查看单个GTD
	 * http://tool.easywan.info/users/work/gtdCheck.html?id=
	 * 
	 */
	@RequestMapping("work/gtdCheck")
	public String gtdCehck(HttpServletRequest request,ModelMap model){
		try{
		Integer id = getInt(request, "id");
		Gtd gtd = gtdService.getByUsersIdAndId(getUsersId(request),id);
		model.addAttribute("gtd",gtd);
		}catch(Exception e){
			logger.error("查看gtd:"+getUserInfo(request,"")+e.getMessage());
			e.printStackTrace();
		}
		return toPath("users/work/gtd/idea/gtd.jsp");
	}
	/**
	 * gtd修改页面
	 * http://tool.easywan.info/users/work/gtdModify.html?id=2
	 */
	@RequestMapping("work/gtdModify")
	public String gtdModity(HttpServletRequest request,ModelMap model){
		try{
			Integer id = getInt(request, "id");
			Gtd gtd = gtdService.getByUsersIdAndId(getUsersId(request),id);
			model.addAttribute("gtd",gtd);
			}catch(Exception e){
				logger.error("查看gtd:"+getUserInfo(request,"")+e.getMessage());
				e.printStackTrace();
			}
		return toPath("users/work/gtd/idea/modify.jsp");
	}
	@ResponseBody
	@RequestMapping("gtd/modifyGtd")
	public String modifyGtd(HttpServletRequest request){
		IResult rst = null;
		try{
		String name = getString(request,"name");
		String content = getString(request,"content");
		String startDate = getString(request,"startDate");
		String endDate = getString(request,"endDate");
		Integer uid = getUsersId(request);
		Integer id = getInt(request, "id");
		rst = gtdService.modifyGtd(name,content,uid,id,startDate,endDate);
		}catch(Exception e){
			logger.error(e.getMessage());
			rst = IResultSupport.buildResult(IResult.FAILT_CODE, e.getMessage());
		}
		return toJSON(rst);
	}
	@ResponseBody
	@RequestMapping("gtdDel")
	public String getDel(HttpServletRequest request){
		IResult rst = null;
		try{
			Integer id = getInt(request, "id");
			rst = gtdService.delToTrash(getUsersId(request),id);
		}catch(Exception e){
			logger.error(e.getMessage());
			rst = IResultSupport.buildResult(IResult.FAILT_CODE, e.getMessage());
		}
		return toJSON(rst);
	}
	@ResponseBody
	@RequestMapping("gtdFinish")
	public String gtdFinish(HttpServletRequest request){
		IResult rst = null;
		try{
			Integer id = getInt(request, "id");
			rst = gtdService.toFinish(getUsersId(request),id);
		}catch(Exception e){
			logger.error(e.getMessage());
			rst = IResultSupport.buildResult(IResult.FAILT_CODE, e.getMessage());
		}
		return toJSON(rst);
	}
	@ResponseBody
	@RequestMapping("gtdStart")
	public String gtdStart(HttpServletRequest request){
		IResult rst = null;
		try{
			Integer id = getInt(request, "id");
			rst = gtdService.toStart(getUsersId(request),id);
		}catch(Exception e){
			logger.error(e.getMessage());
			rst = IResultSupport.buildResult(IResult.FAILT_CODE, e.getMessage());
		}
		return toJSON(rst);
	}
}
